from urllib import request
from Big_Data_View import models
# 加载Modbus模块
import modbus_tk.modbus_tcp  as mt
import modbus_tk.defines as md
from django.shortcuts import render
def factory_venu(request):
    # 配置点位1
    modbus_connect = mt.TcpMaster("192.168.1.42", 502)
    modbus_connect.set_timeout(5.0)
    data_all = modbus_connect.execute(1, md.READ_HOLDING_REGISTERS, 0, 3)
    data_all = list(data_all)
    one_w_name = "one_w"
    one_w_value = data_all[1]
    print(one_w_value)
    judge_existence=models.factory_env_data.objects.filter(env_name="one_w").count()
    #判断点位1是否已经存在，存在的话就更改数据，不存在就写入数据
    if judge_existence == 0 :
        write_data = models.factory_env_data(env_name=one_w_name, data_value=one_w_value)
        write_data.save()
    elif judge_existence == 1 :
        update = models.factory_env_data.objects.get(env_name=one_w_name)
        update.data_value = one_w_value
        update.save()
    return render(request,"factory_venu.html",{'factory_venu_data_all':data_all})